var app = getApp()

var onHandOver = false //是否正在洗牌
var portWork = false
var pockerData = {
    cz: [{
            cover: '',
            pic: '',
            name: '奖品1',
            val: '',
            active: false
        }, {
            cover: '',
            pic: '',
            name: '奖品2',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品3',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品4',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品5',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品6',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品7',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品8',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '奖品9',
            val: ''
        }
    ],
    sz: [{
            cover: 'asd',
            pic: 'asd',
            name: 'asd',
            val: 'asd'
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        }
    ],
    rx: [{
            cover: '123',
            pic: '123',
            name: '123',
            val: '123'
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        },
        {
            cover: '',
            pic: '',
            name: '',
            val: ''
        }
    ]
}

var awardName = ''
var awardType = 'cz'
var canTogglePort = true

function computedAwardIndex(awardName) { //根据返回的奖励码计算获得什么奖励
    return pockerData[awardType].findIndex(el => el.name === awardName)
}

Page({
    data: {
        onlineNumber: 20000 - Math.floor(Math.random() * 5000),
        onPreRead: true,
        count: 0, //剩余的次数
        onShuffle: false, //在洗牌
        onCollect: false, //在选择
        porkChoosed: true, //
        userInfo: {},
        nodePockerClass: 'cz',
        pockerInfos: pockerData.cz,
        nodes: [{
            type: 'node',
            name: 'div',
            attrs: {
                class: 'spinner'
            }
        }],
        porkStyle: ['left: -194rpx;top:210rpx;', 'top:210rpx;left: 0;', 'top:210rpx;left: 194rpx;', 'top: 0;left: -194rpx;', '', 'top: 0;left: 194rpx;', 'left: -194rpx;top:-210rpx;', 'top:-210rpx;left: 0;', 'left: 194rpx;top:-210rpx;']
    },
    closePreRead() {
        this.setData({ 'onPreRead': false })
    },
    pockerInfoChange(e) {
        var porkChoosed = this.data.porkChoosed
        var _this = this
        if (!porkChoosed) {
            wx.showModal({
                title: '提示',
                content: '放弃这次翻牌抽奖的机会',
                success: function(res) {
                    if (res.confirm) {
                        toggleTab()
                    }
                }
            })
            return
        }
        if (portWork) { return }

        function toggleTab() {
            var index = e.target.dataset.index
            awardType = index
            _this.setData({
                pockerInfos: pockerData[index],
                nodePockerClass: index,
                onShuffle: false,
                onCollect: false,
                porkChoosed: true
            })
        }
        toggleTab()
    },
    onShareAppMessage(op) {
        return {
            success() {},
            fail() {}
        }
    },
    stratTap() {
        if (portWork) { return }
        if (!this.data.count) {
            wx.showModal({
                title: '提示',
                content: '您已经没有抽奖机会了,请联系XXXX',
                confirmText: '确定',
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
            return
        }
        var _this = this
            //开始洗牌
        function start(startOverfn) {
            var shuffleOver = false
            var requestOver = false

            function shuffleStart() {
                portWork = true
                _this.setData({ 'porkChoosed': false, 'onCollect': true })
                setTimeout(() => {
                    _this.setData({ 'onShuffle': true })
                    shuffleOver = true
                    if (requestOver) {
                        startOverfn && startOverfn()
                    }
                }, 1100)
            }

            function requestAwar() {
                app.fn.post({
                    url: 'getArawd',
                    data: {
                        type: awardType
                    },
                    success(data) {
                        if (!data.code) {
                            awardName = data.award
                        }
                        requestOver = true
                        if (shuffleOver) {
                            startOverfn && startOverfn()
                        }
                    },
                    fail() {
                        requestOver = true
                        if (shuffleOver && requestOver) {
                            startOverfn && startOverfn()
                        }
                    }
                })

            }
            shuffleStart()
            requestAwar()
        }
        //洗牌结束
        function shuffleOver() {
            _this.setData({ 'onShuffle': false })
            setTimeout(() => {
                _this.setData({ 'onCollect': false });
            }, 1000)
            setTimeout(() => {
                portWork = false
                canTogglePort = true
                _this.setData({ 'count': _this.data.count - 1 })
            }, 2500)
        }
        start(shuffleOver)
    },
    choosePork(e) {
        if (portWork) { return }
        var awardIndex = computedAwardIndex(awardName)
        var awardArry = pockerData[awardType]
        var index = e.target.dataset.index
        var _this = this
        canTogglePort = false
        var award = awardArry.splice(awardIndex, 1)[0]
        awardArry = awardArry.sort(() => Math.random() - 0.5 > 0)
        awardArry.splice(index, 0, award)
        this.setData({
            pockerInfos: awardArry
        })
        app.fn.post({
            url: 'confirmAward',
            success() {
                wx.showModal({
                    title: '提示',
                    content: '恭喜您获得了' + award.name,
                    confirmText: '确定',
                    showCancel: false,
                    success() {
                        _this.setData({
                            onShuffle: false,
                            onCollect: false,
                            porkChoosed: true
                        })
                    }
                })
            }
        })
    },
    bindViewTap: function() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    getMoreChance() {
        wx.showModal({
            title: '测试阶段',
            content: '测试阶段有需要的请联系XXX',
            showCancel: false
        })
    },
    onLoad: function() {
        var that = this
        setInterval(function() {
                that.setData({ 'onlineNumber': 20000 - Math.floor(Math.random() * 5000) })
            }, 10000)
            // app.fn.post({
            //     url: 'checkExist',
            //     success(data) {
            //         if (!data.code) {
            //             var userData = data.user
            //             console.log(data)
            //             that.setData({ 'count': userData.chanceNumber })
            //         } else {
            //             wx.removeStorage({ key: 'key' })
            //             app.config.key = ''
            //             wx.showModal({
            //                 title: '用户信息无法确认，请重新进入活动',
            //                 showCancel: false,
            //                 success() {
            //                     wx.redirectTo({ url: '../welcomePage/welcomePage' })
            //                 }
            //             })
            //         }
            //     }
            // })
    }
})